﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace BlawkayEngine.Input
{
    public class GamepadInput
    {

        GamePadState _PrevState;
        GamePadState _CurrState;

        PlayerIndex _PlayerIndex;
        bool _IsConnected;

        public GamepadInput(PlayerIndex oPlayerIndex)
        {
            _PrevState = GamePad.GetState(_PlayerIndex);
            _CurrState = GamePad.GetState(_PlayerIndex);

            _PlayerIndex = oPlayerIndex;
            _IsConnected = GamePad.GetCapabilities(_PlayerIndex).IsConnected;
        }

        public void Update()
        {
            _IsConnected = GamePad.GetCapabilities(_PlayerIndex).IsConnected;

            if (_IsConnected)
            {
                _PrevState = _CurrState;
                _CurrState = GamePad.GetState(_PlayerIndex);
            }
        }

        /// <summary>
        /// Determines if the controller is connected to the xbox
        /// </summary>
        public bool IsConnected
        {
            get { return _IsConnected; }
        }

        /// <summary>
        /// Detertines if one of keys inputed have just been pressed
        /// </summary>
        /// <param name="oButtons">Keys to Check</param>
        /// <returns>True if a key has just been pressed</returns>
        public bool JustPressed(params Buttons[] oButtons)
        {
            if (_IsConnected == false)
                return false;

            foreach (Buttons oButton in oButtons)
            {
                bool keyUp = _PrevState.IsButtonUp(oButton);
                bool keyDown = _CurrState.IsButtonDown(oButton);

                if ((keyUp && keyDown) == false)
                    return false;
            }

            return true;
        }

        /// <summary>
        /// Detertines if one of keys inputed have just been released
        /// </summary>
        /// <param name="oButtons">Keys to Check</param>
        /// <returns>True if a key has just been released</returns>
        public bool JustReleased(params Buttons[] oButtons)
        {
            if (_IsConnected == false)
                return false;

            foreach (Buttons oButton in oButtons)
            {
                bool keyDown = _PrevState.IsButtonDown(oButton);
                bool keyUp = _CurrState.IsButtonUp(oButton);

                if ((keyDown && keyUp) == false)
                    return false;
            }

            return true;
        }

        /// <summary>
        /// Detertines if all of keys inputed are being pressed
        /// </summary>
        /// <param name="oButtons">Keys to Check</param>
        /// <returns>True if all the keys are being pressed</returns>
        public bool IsPressed(params Buttons[] oButtons)
        {
            if (_IsConnected == false)
                return false;

            foreach (Buttons oButton in oButtons)
            {
                bool keyDown = _CurrState.IsButtonDown(oButton);

                if (keyDown == false)
                    return false;
            }

            return true;
        }

        /// <summary>
        /// Detertines if all of keys inputed are being released
        /// </summary>
        /// <param name="oButtons">Keys to Check</param>
        /// <returns>True if all the keys are being released</returns>
        public bool IsReleased(params Buttons[] oButtons)
        {
            if (_IsConnected == false)
                return false;

            foreach (Buttons oButton in oButtons)
            {
                bool keyUp = _CurrState.IsButtonUp(oButton);

                if (keyUp == false)
                    return false;
            }

            return true;
        }

        /// <summary>
        /// Detertines if one of keys inputed is pressed
        /// </summary>
        /// <param name="oButtons">Keys to Check</param>
        /// <returns>True if one of the keys is pressed</returns>
        public bool IsOnePressed(params Buttons[] oButtons)
        {
            if (_IsConnected == false)
                return false;

            foreach (Buttons oButton in oButtons)
            {
                bool keyDown = _CurrState.IsButtonDown(oButton);

                if (keyDown == false)
                    return false;
            }

            return true;
        }

        /// <summary>
        /// Detertines if one of keys inputed is released
        /// </summary>
        /// <param name="oButtons">Keys to Check</param>
        /// <returns>True if one of the keys is released</returns>
        public bool IsOneReleased(params Buttons[] oButtons)
        {
            if (_IsConnected == false)
                return false;

            foreach (Buttons oButton in oButtons)
            {
                bool keyUp = _CurrState.IsButtonUp(oButton);

                if (keyUp == false)
                    return false;
            }

            return true;
        }

    }
}
